<template>
  <view>
    <scroll-view scroll-y="true">
      <view class="topbg"></view>
      <div class="title">健康数据</div>
      <bindDevice :dList="dList" />
      <healthData
        :blockData="blockData"
        :uricAcidData="uricAcidData"
        :uricketoneData="uricKetone"
        @goPage="toPage"
      />
      <dataMore @goPage="toBaisuiyuanPage" />
      <view style="height: 60upx"> </view>
    </scroll-view>
  </view>
</template>

<script>
import healthData from "./components/healthData.vue";
import bindDevice from "./components/bindDevice.vue";
import dataMore from "./components/dataMore.vue";
import {
  getDeviceList,
  openWebView,
  getBlockData,
  queryUricAcidBlockData,
  queryUricKetoneBlockData,
  uploadSteps,
} from "@/sg/index";

import { getData } from "./bsyData";

export default {
  components: {
    bindDevice,
    healthData,
    dataMore,
  },
  data() {
    return {
      dList: [],
      blockData: undefined,
      uricAcidData: undefined,
      uricKetone: undefined,
      familyList: [], //家人列表
      weRunStatus: 1,
      bsyData: getData()
    };
  },
  onShow() {
    console.log(global.userInfo, "lou");
    // 未登录不调用接口
    if (global.token) {
      this.getFamilyList();
      setTimeout(() => {
        this.getDeviceInfo();
        this.getBlockData();
        this.getFamilyList();
        // this.getUricAcidData();
        // this.getUricKetoneData();
        // this.indicatorList();
      }, 1000);
    }
  },
  onPullDownRefresh() {
    setTimeout(function () {
      uni.stopPullDownRefresh();
    }, 1000);
    if (global.token != "") {
      this.getDeviceInfo();
      this.getBlockData();
      this.getFamilyList();
      // this.getUricAcidData();
      // this.getUricKetoneData();
      // this.indicatorList();
    }
  },
  methods: {
    indicatorList() {
      const params = {
        hisUserId: global.userInfo.userId,
      };
      let indicatorListUrl = global.apiUrls.indicatorList;
      let curveUrl = global.apiUrls.getCurve;
      // this.$https.get(indicatorListUrl, params).then((res) => {
      //   if (res.data.code == 1000) {
      this.$https.get(curveUrl, params).then((res) => {
        console.log(res.data.result, "卡片数据是多少");
        this.bsyData = res.data.result;
      });
      //   }
      // });
    },
    getDeviceInfo() {
      getDeviceList().then((res) => {
        console.warn("getDeviceInfo", res);
        this.dList = res.data.devices;
      });
    },
    getBlockData() {
      getBlockData().then((res) => {
        this.blockData = res.data;
      });
    },
    getUricAcidData() {
      queryUricAcidBlockData().then((res) => {
        console.warn("sssss", res);
        this.uricAcidData = res.data;
      });
    },
    getUricKetoneData() {
      queryUricKetoneBlockData().then((res) => {
        this.uricKetone = res.data;
      });
    },
    toPage(item, idx) {
      if (global.token) {
        if (item.type == "bsy") {
          const data = this.getData(item);
          if (data) {
            this.$urouter.navigateTo({
              url: item.path,
              params: {
                data,
              },
            });
          }
        } else if (item.type == "step") {
          this.goStep();
        } else {
          openWebView(item.path);
        }
      } else {
        this.$basejs.globaltoken();
      }
    },
    goStep() {
      const path = "/step/main.html";
      if (global.weSportsCancel === true) {
        // 取消关注微信运动
        uploadSteps(null, true).then((res) => {
          openWebView(path);
        });
      } else if (global.weRunStatus === 1) {
        // 已授权
        openWebView(path);
      } else if (global.weRunStatus === 2) {
        // 已拒绝
        wx.openSetting({
          success: (res) => {
            if (res.authSetting["scope.werun"]) {
              this.weRunStatus = global.weRunStatus = 1;
            } else if (!res.authSetting["scope.werun"]) {
              wx.showToast({
                title: "请在设置页面打开步数权限~",
                icon: "none",
                duration: 2000,
              });
            }
          },
        });
      } else {
        // 当前为微信步数从未授权状态
        uploadSteps()
          .then((_) => {
            // 已授权
            openWebView(path);
          })
          .catch((_) => {
            // 拒绝授权
            this.weRunStatus = global.weRunStatus = 2;
          });
      }
    },
    toBaisuiyuanPage(val) {
      console.warn("toBaisuiyuanPage");

      if (!global.token) {
        this.$basejs.globaltoken();
        return;
      }
      if (val.name === "家人互联") {
        if (this.familyList.length === 0 || !this.familyList) {
          this.$urouter.navigateTo(
            "/pagesHome/family-connectivity/family-connectivity"
          );
        } else {
          this.$urouter.navigateTo({
            url: "/pagesHome/family-connectivity/family-internet-list",
            params: {
              anotherUserId: this.familyList[0].anotherUserId,
              index: 0,
            },
          });
        }
      } else {
        this.$urouter.navigateTo(val.page);
      }
    },
    //获取家人列表
    getFamilyList() {
      this.familyList = [];
      this.$https
        .get(global.apiUrls.getFamilyList(global.userInfo.userId))
        .then((res) => {
          if (res.data.code === 1000) {
            this.familyList = res.data.result;
          }
        });
    },
    // 判断当前微信步数授权状态
    checkWeRunStatus() {
      wx.getSetting({
        success: (res) => {
          if (res.authSetting["scope.werun"]) {
            // 已授权
            this.weRunStatus = global.weRunStatus = 1;
            // 上传步数
            uploadSteps(() => {
              this.weSportsCancel = global.weSportsCancel;
              this.getStepTarget();
              this.$emit("updateBlockData"); // 授权之后需要重新获取页面数据
            });
          } else if (res.authSetting["scope.werun"] === undefined) {
            // 未授权
            this.weRunStatus = global.weRunStatus = 0;
          } else if (res.authSetting["scope.werun"] === false) {
            // 已拒绝
            this.weRunStatus = global.weRunStatus = 2;
          }
        },
      });
    },
    // 根据选项获取对应的数据
    getData(item) {
      const data = this.bsyData.find((e) => e.itemCode == item.itemCode);
      if (data) {
        return JSON.stringify(data);
      }
      return null;
    },
  },
};
</script>
<style lang="scss" scoped>
.topbg {
  top: 0;
  width: 750upx;
  height: 472upx;
  background: linear-gradient(
    0deg,
    rgba(5, 118, 145, 0) 0.6%,
    rgba(5, 118, 145, 0.3) 100%
  );

  background-size: 750upx 472upx;
  padding-top: 108upx;
  box-sizing: border-box;
  position: absolute;
  margin-bottom: 118upx;
}

.title {
  position: relative;
  z-index: 1000;
  margin-top: 104upx;
  font-size: 40upx;
  margin-left: 40upx;
  color: #222;
}
</style>
